from j264.tools.boxes.Box import Box
from j264.tools.boxes.Inlet import Inlet
from j264.tools.boxes.Outlet import Outlet
from j264.tools.boxes.Patcher import Patcher


class Subpatcher(Box, Patcher):

    ### INITIALIZER ###

    def __init__(self, inlet_count=0, outlet_count=0):

        Patcher.__init__(self)
        Box.__init__(self, inlet_count, outlet_count)

        assert 0 <= inlet_count
        assert 0 <= outlet_count

        self._inlets = []
        for _ in range(inlet_count):
            inlet = Inlet()
            inlet._switch_parent(self)
            self._inlets.append(inlet)

        self._outlets = []
        for _ in range(outlet_count):
            outlet = Outlet()
            outlet._switch_parent(self)
            self._outlets.append(outlet)

    ### READ-ONLY PUBLIC PROPERTIES ###

    @property
    def boxes(self):
        return self.inlets + self.children + self.outlets

    @property
    def inlet_count(self):
        return len(self._inlets)

    @property
    def inlets(self):
        return tuple(self._inlets)

    @property
    def outlet_count(self):
        return len(self._outlets)

    @property
    def outlets(self):
        return tuple(self._outlets)

    ### PRIVATE METHODS ###

    def _get_json_data(self):
        attrs = self.max_attributes
        attrs['id'] = self.max_obj_id
        patcher = attrs['patcher'] = {}
        patcher['boxes'] = [x.json_data for x in self.boxes]
        patcher['lines'] = [x.json_data for x in self.lines]
        return {self.max_obj_type: attrs}        
